<?php  
class ControllerSaleCouponCustomer extends Controller {
	private $error = array();
     
  	public function index() {
		$this->load->language('sale/coupon_customer');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/coupon_customer');
		$this->getList();
  	}
  
  	public function insert() {
    	$this->load->language('sale/coupon_customer');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/coupon_customer');
    	if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
    		$post_data = $this->request->post;
    		if(isset($post_data['customer']) && isset($post_data['coupon_rule_id'])){
				$this->load->model('sale/coupon_rule');
    			$coupon_rule = $this->model_sale_coupon_rule->getCouponRule($post_data['coupon_rule_id']);
    			$coupon_rule_desc = $this->model_sale_coupon_rule->getCouponRuleDescription($post_data['coupon_rule_id']);
	    		foreach ($post_data['customer'] as $customer) {
					$coupon_code = $this->model_sale_coupon_customer->addCouponCustomer($customer['customer_id'], $coupon_rule, $coupon_rule_desc);
					$this->sendCouponCustomer($customer['customer_id'], $coupon_code);
				}
    		}
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('sale/coupon_customer', 'token=' . $this->session->data['token'], 'SSL'));
    	}
    	$this->getForm();
  	}

  	private function getList() {
  		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['text_discount_type_0'] = $this->language->get('text_discount_type_0');
		$this->data['text_discount_type_1'] = $this->language->get('text_discount_type_1');
		$this->data['text_discount_type_2'] = $this->language->get('text_discount_type_2');
		
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_code'] = $this->language->get('column_code');
		$this->data['column_discount_type'] = $this->language->get('column_discount_type');
		$this->data['column_discount_points'] = $this->language->get('column_discount_points');
		$this->data['column_discount_values'] = $this->language->get('column_discount_values');
		$this->data['column_max_use_time'] = $this->language->get('column_max_use_time');
		$this->data['column_date_start'] = $this->language->get('column_date_start');
		$this->data['column_date_end'] = $this->language->get('column_date_end');
		$this->data['column_category_ids'] = $this->language->get('column_category_ids');
		$this->data['column_customer_name'] = $this->language->get('column_customer_name');
		$this->data['column_email'] = $this->language->get('column_email');
		
		$this->data['button_insert'] = $this->language->get('button_insert');
 		$this->data['button_filter'] = $this->language->get('button_filter');
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/coupon_customer', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('sale/coupon_customer/insert', 'token=' . $this->session->data['token'], 'SSL');
		
  		if (isset($this->request->get['filter_customer_id'])) {
			$filter_customer_id = $this->request->get['filter_customer_id'];
		}else{
			$filter_customer_id = null;
		}
  		if (isset($this->request->get['filter_code'])) {
			$filter_code = $this->request->get['filter_code'];
		}else{
			$filter_code = null;
		}
  		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		}else{
			$filter_name = null;
		}
  		if (isset($this->request->get['filter_email'])) {
			$filter_email = $this->request->get['filter_email'];
		}else{
			$filter_email = null;
		}
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'c.max_use_time';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'DESC';
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
				
		$data = array(
			'filter_customer_id'  => $filter_customer_id,
			'filter_code'  => $filter_code,
			'filter_name' => $filter_name,
			'filter_email'  => $filter_email,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		$coupon_customer_total = $this->model_sale_coupon_customer->getTotalCouponCustomers($data);
 		$this->data['coupon_customers'] = $this->model_sale_coupon_customer->getCouponCustomers($data);
									
		$url = '';
  		if (isset($this->request->get['filter_customer_id'])) {
			$url .= '&filter_customer_id=' . $this->request->get['filter_customer_id'];
		}
  		if (isset($this->request->get['filter_code'])) {
			$url .= '&filter_code=' . $this->request->get['filter_code'];
		}
  		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}
  		if (isset($this->request->get['filter_email'])) {
			$url .= '&filter_email=' . $this->request->get['filter_email'];
		}
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		$this->data['sort_code'] = $this->url->link('sale/coupon_customer', 'token=' . $this->session->data['token'] . '&sort=c.code' . $url, 'SSL');
		$this->data['sort_max_use_time'] = $this->url->link('sale/coupon_customer', 'token=' . $this->session->data['token'] . '&sort=c.max_use_time' . $url, 'SSL');
		$this->data['sort_date_start'] = $this->url->link('sale/coupon_customer', 'token=' . $this->session->data['token'] . '&sort=c.date_start' . $url, 'SSL');
		$this->data['sort_date_end'] = $this->url->link('sale/coupon_customer', 'token=' . $this->session->data['token'] . '&sort=c.date_end' . $url, 'SSL');
				
		$url = '';
  		if (isset($this->request->get['filter_customer_id'])) {
			$url .= '&filter_customer_id=' . $this->request->get['filter_customer_id'];
		}
  		if (isset($this->request->get['filter_code'])) {
			$url .= '&filter_code=' . $this->request->get['filter_code'];
		}
  		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}
  		if (isset($this->request->get['filter_email'])) {
			$url .= '&filter_email=' . $this->request->get['filter_email'];
		}
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $coupon_customer_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('sale/coupon_customer', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();

		$this->data['filter_customer_id'] = $filter_customer_id;
		$this->data['filter_code'] = $filter_code;
		$this->data['filter_name'] = $filter_name;
		$this->data['filter_email'] = $filter_email;
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		$this->data['token'] = $this->session->data['token'];

		$this->template = 'sale/coupon_customer_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
  	}

  	private function getForm() {
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_coupon_search'] = $this->language->get('text_coupon_search');
		$this->data['text_customer_search'] = $this->language->get('text_customer_search');
    	$this->data['text_discount_type_0'] = $this->language->get('text_discount_type_0');
		$this->data['text_discount_type_1'] = $this->language->get('text_discount_type_1');
		$this->data['text_discount_type_2'] = $this->language->get('text_discount_type_2');
		
		$this->data['column_customer_name'] = $this->language->get('column_customer_name');
		$this->data['column_email'] = $this->language->get('column_email');
		$this->data['column_grade_name'] = $this->language->get('column_grade_name');
		
    	$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_discount_type'] = $this->language->get('entry_discount_type');
		$this->data['entry_discount_points'] = $this->language->get('entry_discount_points');
		$this->data['entry_discount_values'] = $this->language->get('entry_discount_values');
		$this->data['entry_max_use_time'] = $this->language->get('entry_max_use_time');
		$this->data['entry_day_use'] = $this->language->get('entry_day_use');
		$this->data['entry_category_ids'] = $this->language->get('entry_category_ids');
		$this->data['entry_customer_first_name'] = $this->language->get('entry_customer_first_name');
		$this->data['entry_customer_last_name'] = $this->language->get('entry_customer_last_name');
		$this->data['entry_email'] = $this->language->get('entry_email');

    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_remove'] = $this->language->get('button_cancel');
		
		$this->data['tab_coupon'] = $this->language->get('tab_coupon');
		$this->data['tab_customer'] = $this->language->get('tab_customer');
		
		$this->data['token'] = $this->session->data['token'];
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/coupon_customer', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['action'] = $this->url->link('sale/coupon_customer/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('sale/coupon_customer', 'token=' . $this->session->data['token'], 'SSL');
		
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		$this->template = 'sale/coupon_customer_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());		
  	}
	
	private function sendCouponCustomer($customer_id, $coupon_code) {
		$this->load->model('customer/customer');
		$customer_info = $this->model_customer_customer->getCustomer($customer_id);
		
		$coupon = $this->model_sale_coupon_customer->getCouponCustomerByCode($customer_id, $coupon_code);
		
		$template = new Template();	
		$template->data['store_name'] = $this->config->get('config_name');
		$template->data['store_url'] = HTTP_CATALOG;
		$template->data['name'] = $customer_info['firstname'];
		$template->data['code'] = $customer_info['code'];
		$html = $template->fetch('mail/coupon_customer_' . $customer_info['language_code'] . '.tpl');
		
					
		$mail = new Mail(); 
		$mail->defaultSender();
		$mail->sendMsg("test", $coupon['name'], null, $html, null, $customer_info['email']);
	}
	
}
?>